ihelpuoo.namespace("widget.pattern");

var VIEW_NAME_ATT = "view-name";

var ihelpuoo_widget_views = ihelpuoo.util.map();

ihelpuoo.widget.addViewEventByKey = function(view,key,eventName,fnHandler,bCapture)
{
	var ele = view.getElement(key);
	if(ele)
	{
		ihelpuoo.widget.setViewNameToElement(ele,view);
		ihelpuoo.event.add(ele,eventName,fnHandler,bCapture);
	}	
}

ihelpuoo.widget.addViewEvent = function(view,ele,eventName,fnHandler,bCapture)
{
	ihelpuoo.widget.setViewNameToElement(ele,view);
	ihelpuoo.event.add(ele,eventName,fnHandler,bCapture);	
}

ihelpuoo.widget.setViewNameToElementByKey = function(key,view)
{
	var ele = view.	getElement(key);
	ihelpuoo.widget.setViewNameToElement(ele,view);
}

ihelpuoo.widget.setViewNameToElement = function(ele,view)
{
	ele.setAttribute(VIEW_NAME_ATT,view.name);
}

ihelpuoo.widget.getViewByEventSource = function(obj)
{
	return ihelpuoo.widget.getViewByElement(ihelpuoo.event.srcElement(obj));
}

ihelpuoo.widget.getViewByElement = function(ele)
{
	var viewName = ele.getAttribute(VIEW_NAME_ATT);
	if(ihelpuoo.isEmpty(viewName))
	{
		if(ele.parentNode)
			return ihelpuoo.widget.getViewByElement(ele.parentNode);
		return null;
	}
	return ihelpuoo_widget_views.get(viewName);
}

ihelpuoo.widget.getView = function(name)
{
	return ihelpuoo_widget_views.get(name);
}

ihelpuoo.widget.containsView = function(name)
{
	return ihelpuoo_widget_views.containsKey(name);	
}

ihelpuoo.widget.removeView = function(name)
{
	return ihelpuoo_widget_views.remove(name);
}

ihelpuoo.widget.removeAllView = function(name)
{
	return ihelpuoo_widget_views.clear();
}

ihelpuoo.widget.registerView = function(name,view)
{
	if(null==view)
		ihelpuoo.warn("View object is NULL when registerView()");
	else if(!name)
	{
		ihelpuoo.warn("Fail to register anonymous view!");
	}
	else
	{
		if(ihelpuoo.isDebugEnabled() && ihelpuoo.widget.containsView(view.name))
				ihelpuoo.debug("Override view [" + name + "]");
		ihelpuoo_widget_views.put(name,view);
	}
	return view;
}

ihelpuoo.widget.registerView2 = function(view)
{
	ihelpuoo.widget.registerView(view.name,view);
}

ihelpuoo.widget.View = function(gasket,patternName,name)
{
	this.name = name;
	this.gasket = gasket;
	this.pattern = null;
	this.patternName = patternName;
	this.shell = null;
}

ihelpuoo.widget.View.prototype
=
{
	draw: function(obj)
	{
		if(this.isNeverBeDrawn())
		{
			this.preparePattern();
		}
		this.display();
		if(obj)
			this.pattern.fillByObject(obj);
	}
		
	
	
	,
	isNeverBeDrawn: function()
	{
		return null==this.pattern;
	}
	
	,
	preparePattern: function()
	{
		if(this.gasket==null)
		{
			ihelpuoo.error("ihelpuoo.widget.View expect a valid Gasket!");
			return null;
		}
		if(null==this.pattern)
		{
			this.pattern = ihelpuoo.widget.pattern.create(this.patternName);
			this.shell = this.pattern.domNodes[0];
			this.hide();
			this.pattern.attachTo(this.gasket);
		}
	}
	
	,
	display: function()
	{
		if(this.shell)
			ihelpuoo.dom.showElement(this.shell);
	}
	
	,
	hide: function()
	{
		if(this.shell)
			this.shell.style.display = "none";
	}
	
	,
	isHidden: function()
	{
		if(!this.shell) return true;
		return this.shell.style.display == "none";
	}
	
	,
	focusElement: function(key)
	{
		var ele = this.getElement(key);
		if(ele)
		{
			try{ele.focus();}catch(E){}
		}
	}
	
	,
	disableElement: function(key)
	{
		var ele = this.getElement(key);
		if(ele)
			ihelpuoo.dom.disableElement(ele);
	}
	
	,
	enableElement: function(key)
	{
		var ele = this.getElement(key);
		if(ele)
			ihelpuoo.dom.enableElement(ele);
	}
	
	,
	getElement: function(key)
	{
		if(null==this.pattern)
		{
			ihelpuoo.warn("Fail to find \""+key+"\" in pattern: " + this.patternName);
			return null;
		}
		return this.pattern.getElementByPathkey(key);
	}
	
	,
	get: function(key,def)
	{
		if(null==this.pattern)return (def?def:"");
		var ele = this.pattern.getElementByPathkey(key);
		if(null==ele)return (def?def:"");
		var v = ihelpuoo.dom.autoGetString(ele);
		if(ihelpuoo.isEmpty(v))
			if(def)
				return def;
			else
				return "";
		return v;
	}
	
	,
	encodeTrimString: function(key,def)
	{
		return encodeURIComponent(ihelpuoo.trim(this.get(key,def)));
	}
	
	,
	trimString: function(key,def)
	{
		return ihelpuoo.trim(this.get(key,def));
	}
	
	,
	getString: function(key,def)
	{
		return encodeURIComponent(this.get(key,def));
	}
	
	,
	getInt: function(key,def)
	{
		var v = this.getString(key);
		return ihelpuoo.parseInt(v,def);
	}
	
	,
	getBoolean: function(key,def)
	{
		var v = this.getString(key);
		return ihelpuoo.parseBoolean(v,def);
	}
	
	,
	set: function(key,value)
	{
		var ele = this.pattern.getElementByPathkey(key);
		ihelpuoo.dom.autoSetObject(ele,value);
	}
	
	,
	addEvent: function(key,eventName,fnHandler,bCapture)
	{
		var ele = this.getElement(key);
		ihelpuoo.widget.setViewNameToElement(ele,this);
		ihelpuoo.event.add(ele,eventName,fnHandler,bCapture);	
	}
	
	,
	removeEvent: function(key,eventName,fnHandler,bCapture)
	{
		var ele = this.getElement(key);
		ihelpuoo.event.remove(ele,eventName,fnHandler,bCapture);	
	}
	
}